<!DOCTYPE HTML>
<link rel="help" href="http://url.spec.whatwg.org/#dom-url-hash">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>

test(function() {
    var url = new URL('http://www.domain.com/');
    assert_equals(url.hash, '');

    url = new URL('http://www.domain.com/#');
    assert_equals(url.hash, '');

    url = new URL('http://www.domain.com/#a');
    assert_equals(url.hash, '#a');

    url.hash = '';
    assert_equals(url.hash, '');
    assert_equals(url.toString(), 'http://www.domain.com/');

    url.hash = '#';
    assert_equals(url.hash, '');
    assert_equals(url.toString(), 'http://www.domain.com/#');

    url.hash = 'a';
    assert_equals(url.hash, '#a');

    url.hash = '#a';
    assert_equals(url.hash, '#a');

    url.hash = 'a#b';
    assert_equals(url.hash, '#a#b');
}, 'Basic hash');

test(function() {
    var url = new URL('mailto:foo@bar.com');
    assert_equals(url.hash, '');

    url.hash = 'a';
    assert_equals(url.hash, '');
}, 'non hierarchical');

test(function() {
    var url = new URL('http://abc.de:8080/path/file?query#fragment');
    var expected = '#fragment';
    assert_equals(url.hash, expected);

    assert_throws_js(TypeError, () => { url.href = 'invalid'; });
    assert_equals(url.hash, expected);

    url.hash = 'changed';
    assert_equals(url.hash, '#changed');
    assert_equals(url.href, 'http://abc.de:8080/path/file?query#changed');
}, 'hash property invalid URL');

test(function() {
    var url = new URL('http://www.domain.com/');
    assert_equals(url.hash, '');

    url.hash = '\udc01\ud802a';
    assert_equals(url.hash, '#%EF%BF%BD%EF%BF%BDa');
}, 'hash with unmatched surrogates');

</script>
